import os
import posixpath
import stat

from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.trade_vo import TradePageQueryModel, TradeModel, DeleteTradeModel
from module_kylin.service.trade_service import TradeService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

from config import env
import paramiko
import fnmatch  # 导入fnmatch模块用于通配符匹配

tradeController = APIRouter(prefix='/kylin/trade', dependencies=[Depends(LoginService.get_current_user)])


@tradeController.get('/list', response_model=PageResponseModel,
                     dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:list'))])
async def get_kylin_trade_list(
        request: Request,
        trade_page_query: TradePageQueryModel = Depends(TradePageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    trade_page_query_result = await TradeService.get_trade_list_services(query_db, trade_page_query, is_page=True)
    logger.info('获取成功')
    return ResponseUtil.success(model_content=trade_page_query_result)


@tradeController.get('/download', dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:download'))])
async def download_kylin_trade(request: Request, server_id: str):
    """
    :param server_id: 服务器配置标识
    """
    # 调用service层的下载方法
    success, message = await TradeService.download_kylin_trade_service(server_id)
    if success:
        return ResponseUtil.success(msg=message)
    else:
        return ResponseUtil.error(msg=f"下载失败: {message}")


@tradeController.get('/load', dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:load'))])
async def load_kylin_trade(request: Request, server_id: str, query_db: AsyncSession = Depends(get_db)):
    """
    加载交易文件数据到数据库
    :param server_id: 服务器配置标识
    """
    # 调用service层的加载方法
    success, message = await TradeService.load_kylin_trade_service(query_db, server_id)
    if success:
        return ResponseUtil.success(msg=message)
    else:
        return ResponseUtil.error(msg=f"加载失败: {message}")


@tradeController.get('/{id}', response_model=TradeModel,
                     dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:query'))])
async def query_detail_kylin_trade(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    trade_detail_result = await TradeService.trade_detail_services(query_db, id)
    logger.info(f'获取config_id为{id}的信息成功')

    return ResponseUtil.success(data=trade_detail_result)


@tradeController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:add'))])
@ValidateFields(validate_model='add_trade')
@Log(title='成交记录', business_type=BusinessType.INSERT)
async def add_kylin_trade(
        request: Request,
        add_trade: TradeModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_trade_result = await TradeService.add_trade_services(query_db, add_trade)
    logger.info(add_trade_result.message)
    return ResponseUtil.success(msg=add_trade_result.message)


@tradeController.put('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:edit'))])
@ValidateFields(validate_model='edit_config')
@Log(title='成交记录', business_type=BusinessType.UPDATE)
async def edit_kylin_trade(
        request: Request,
        edit_trade: TradeModel,
        query_db: AsyncSession = Depends(get_db),
):
    edit_trade_result = await TradeService.edit_config_services(query_db, edit_trade)
    logger.info(edit_trade_result.message)

    return ResponseUtil.success(msg=edit_trade_result.message)


@tradeController.delete('/{trade_ids}', dependencies=[Depends(CheckUserInterfaceAuth('kylin:trade:remove'))])
@Log(title='成交记录', business_type=BusinessType.DELETE)
async def delete_kylin_trade(request: Request, trade_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_trade = DeleteTradeModel(tradeIds=trade_ids)
    delete_config_result = await TradeService.delete_trade_services(request, query_db, delete_trade)
    logger.info(delete_config_result.message)

    return ResponseUtil.success(msg=delete_config_result.message)